package com.hhdys.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhdys.annotation.Access;
import com.hhdys.model.Function;
import com.hhdys.model.MenuTree;
import com.hhdys.model.RoleFuncAss;
import com.hhdys.service.FunctionService;
import com.hhdys.service.MenuTreeService;
import com.hhdys.service.RoleFuncAssService;
import com.hhdys.service.RoleService;

@Controller
@RequestMapping("/admin/role")
@Access("角色管理")
public class RoleAction {
	private static Logger log = LogManager.getLogger(RoleAction.class);
	@Autowired
	private RoleService roleService;
	@Autowired
	private MenuTreeService menuService;
	@Autowired
	private FunctionService functionService;
	@Autowired
	private RoleFuncAssService assService;

	@RequestMapping("/list")
	@Access("查看")
	public String list(Model model) {
		model.addAttribute("list", roleService.selRoles());
		return "admin/role/roleList";
	}

	@RequestMapping("/add")
	@ResponseBody
	@Access("添加")
	public String addRole(String name) {
		Subject curUser = SecurityUtils.getSubject();
		if (roleService.addRole(name, (String) curUser.getPrincipal())) {
			return "success";
		}
		return "error";
	}

	@RequestMapping("/del")
	@ResponseBody
	@Access("删除")
	public String delRole(String ids) {
		if (roleService.delRoles(ids)) {
			return "success";
		}
		return "error";
	}

	@RequestMapping("/edit")
	@ResponseBody
	@Access("编辑")
	public String editRole(int id, String name) {
		Subject curUser = SecurityUtils.getSubject();
		if (roleService.editRole(id, name, (String) curUser.getPrincipal())) {
			return "success";
		}
		return "error";
	}

	@RequestMapping(value = "menuFunc", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getMenuFunc() {
		StringBuilder builder = new StringBuilder("[");
		List<MenuTree> menuTrees = sortMenus(menuService.showAllTrees(), 0);
		for (MenuTree tree : menuTrees) {
			String name = tree.getName();
			builder.append("{\"name\":\"" + name + "\",\"func\":[");
			List<Function> functions = functionService
					.getFunctionsByPageId(tree.getId());
			for (Function func : functions) {
				builder.append("{\"name\":\"" + func.getName() + "\",\"id\":"
						+ func.getId() + "},");
			}
			if (functions.size() > 0) {
				builder.deleteCharAt(builder.length() - 1);
			}
			builder.append("]},");
		}
		if (menuTrees.size() > 0) {
			builder.deleteCharAt(builder.length() - 1);
		}
		builder.append("]");
		return builder.toString();
	}

	private List<MenuTree> sortMenus(List<MenuTree> list, int parentId) {
		List<MenuTree> result = new ArrayList<MenuTree>();
		for (MenuTree menu : list) {
			if (menu.getParentId() == parentId) {
				result.add(menu);
				result.addAll(sortMenus(list, menu.getId()));
			}
		}
		return result;
	}

	@RequestMapping("/addFuncs")
	@ResponseBody
	@Access("权限管理")
	public String addRoleFuncs(int roleId, String funcs) {
		assService.addAss(roleId, funcs);
		return "success";
	}

	@RequestMapping("/getFuncs")
	@ResponseBody
	public String getFuncs(int roleId) {
		StringBuilder builder = new StringBuilder();
		List<RoleFuncAss> list = assService.getAssByRoleId(roleId);
		for (RoleFuncAss ass : list) {
			builder.append(ass.getFuncId() + ",");
		}
		return builder.toString();
	}

	/**
	 * @return the roleService
	 */
	public RoleService getRoleService() {
		return roleService;
	}

	/**
	 * @param roleService
	 *            the roleService to set
	 */
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * @return the menuService
	 */
	public MenuTreeService getMenuService() {
		return menuService;
	}

	/**
	 * @param menuService
	 *            the menuService to set
	 */
	public void setMenuService(MenuTreeService menuService) {
		this.menuService = menuService;
	}

	/**
	 * @return the functionService
	 */
	public FunctionService getFunctionService() {
		return functionService;
	}

	/**
	 * @param functionService
	 *            the functionService to set
	 */
	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	/**
	 * @return the assService
	 */
	public RoleFuncAssService getAssService() {
		return assService;
	}

	/**
	 * @param assService
	 *            the assService to set
	 */
	public void setAssService(RoleFuncAssService assService) {
		this.assService = assService;
	}
}
